package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);

    public void start() {
        while (true) {
            System.out.println("--------------欢迎来到 <学生> 管理系统----------------------");
            System.out.println("请输入您的选择：1.添加学生 2.删除学生 3.修改学生 4.查看学生 5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    // System.out.println("添加");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改");
                    updateStudentById();
                    break;
                case "4":
                    //System.out.println("查看");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢使用");
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
        }
    }
//删除
    public void deleteStudentById() {
        String id = inputStudent();
        studentService.deleteStudentById(id);
        System.out.println("删除成功");
    }
    //修改
    public void updateStudentById() {
        String id = inputStudent();
        Student stu=inputStudentInfo(id);
        studentService.updateStudentById(stu,id);
        System.out.println("修改成功");
    }
    //查询
    public void findAllStudent() {
        Student[] students = studentService.finAllStudent();
        if (students != null) {
            System.out.println("学号\t姓名\t年龄\t生日");
            for (Student student : students) {
                if (student != null) {
                    System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "\t\t" + student.getBirthday());
                }
            }
        } else {
            System.out.println("暂无数据");
        }

    }
    //添加
    public void addStudent() {
        Scanner sc = new Scanner(System.in);

        String id;
        //判断学号是否存在
        while (true) {
            System.out.println("请输入学生id：");
            id = sc.next();
            if (studentService.isExits(id)) {
                System.out.println("学号已经存在");
            } else {
                break;
            }
        }
        Student stu = inputStudentInfo(id);
        boolean result = studentService.addStudent(stu);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    /**
     * @return 判断学生id 是否存在
     */
    public String inputStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生id:");
            id = sc.next();
            if (!studentService.isExits(id)) {
                System.out.println("输入的学生id不存在，请重新录入");
            } else {
                break;
            }
        }
        return id;
    }

    /**
     * @param id 学生id
     * @return 学生类
     */
    public Student inputStudentInfo(String id) {
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }
}
